<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
        }
        div {
            position: relative;
            width: 500px;
            height: 400px;
            background-color: red;
        }

        h2 {
            position: absolute;
            right: 50%;
            top: 50%;
            width: 200px;
            height: 150px;
            background-color: #ccc;
            /* 以下是以前的做法，  缺点：把margin值直接写死了，如果后期盒子的大小变了，就不太好 */
            /* margin-right: -100px;
            margin-top: -75px; */

            /* 用translate来做就很nice */
            transform: translate(50%,-50%);
            


        }
    </style>
</head>

<body>
    <div>
        <h2></h2>
    </div>

</body>

</html>